package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.constant.AccountType;
import com.qiche.dto.UserDto;
import com.qiche.model.*;
import com.qiche.service.*;
import com.qiche.util.PasswordHelper;
import com.qiche.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店员管理管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/clerks", description = "店员管理管理控制器")
@RestController
@RequestMapping("/clerks")
public class ClerkController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MerchantService merchantService;

    @PostMapping("/add")
    public Result<User> add(@RequestBody User user) {
        boolean exist = userService.isUsernameExist(user.getUsername());
        if (exist) {
            return Result.createByErrorMessage("用户名已经存在");
        }
        User currentUser = UserUtils.getCurrentUser();
        //默认为店员类型
        user.setAccountType(AccountType.CLERK);
        user.setMerchantId(currentUser.getMerchantId());
        user.setEnable(1);
        //密码加密
        PasswordHelper helper = new PasswordHelper();
        helper.encryptPassword(user);

        int resultCount = userService.save(user);
        //添加 用户 -角色 关联
        if (user.getRoleId() != null) {
            UserRole userRole = new UserRole();
            userRole.setRoleid(user.getRoleId().toString());
            userRole.setUserid(user.getId());
            userRoleService.addUserRole(userRole);
        }
        if (resultCount == 0) {
            return Result.createByErrorMessage("添加失败");
        }
        return Result.createBySuccessMessage("添加成功");
    }


    @RequestMapping
    public Map<String, Object> getAll(User user,
                                      @RequestParam(required = false, defaultValue = "1") int page,
                                      @RequestParam(required = false, defaultValue = "10") int limit) {
        Map<String, Object> map = new HashMap<>();
        User currentUser = UserUtils.getCurrentUser();
        user.setMerchantId(currentUser.getMerchantId());
        user.setEnable(1);
        PageInfo<User> pageInfo = userService.selectClerksByPage(user, page, limit);

        List<UserDto> dtos = new ArrayList<UserDto>();

        pageInfo.getList().forEach(userTemp -> {
            Role role = roleService.queryRoleByUserId(userTemp.getId());
            UserDto dto = new UserDto();
            BeanUtils.copyProperties(userTemp, dto);
            if (role != null) {
                dto.setRoleName(role.getRoleName());
            }
            if (userTemp.getMerchantId() != null) {
                Merchant merchant = merchantService.selectById(userTemp.getMerchantId().longValue());
                if (merchant != null) {
                    dto.setMerchantName(merchant.getName());
                }

            }
            dtos.add(dto);
        });

        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", dtos);

        return map;
    }

}


